//---------------------------------------------------------------------------

#include <vcl.h>
#include <windef.h>
#pragma hdrstop

#include "Unit1.h"
#include "UnitInspector.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
 iObject = 0;
 Form1->Show();
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
 b1 = new TButton(Form1->Button2);
 b1->Parent = Panel2;
 //b1->OnClick = Button2Click;
 b1->OnMouseMove= Button2MouseMove;
 b1->OnMouseUp = Button2MouseUp;
 b1->OnMouseDown = Button2MouseDown;

 b1->PopupMenu = PopupMenu1;
 b1->PopupMenu->OnPopup = PopupMenu1Popup;

 iObject++;
 b1->Name ="BTest"+IntToStr(iObject);
 b1->Caption = "Test"+IntToStr(iObject);

 b1->Visible=true;
 b1->Left =10;
 b1->Top=10;
 b1->Enabled=true;
 listObj.insert(listObj.begin(),b1);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
 AnsiString sO;
 sO = Sender->ClassName();
 //MessageBox(NULL,sO.c_str(),"!",MB_OK);
 //MoveObject=!MoveObject;
 bObject =  (TButton*)Sender;
 cObject =  Sender;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Panel2MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
 Label1->Caption = "Panel X:"+IntToStr(X)+" Y:"+IntToStr(Y);
 if ( MoveObject && false )
 {
  bObject->Left = X;
  bObject->Top = Y;
 }
 if ( ResizeObject )
 {
  bObject->Width = (X-bObject->Left>0?X-bObject->Left:bObject->Width);
  bObject->Height = (Y-bObject->Top>0?Y-bObject->Top:bObject->Height);
 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Panel2MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
 //MoveObject = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N1Click(TObject *Sender)
{
 Shape1->Visible =false;
 delete bObject;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
        //TButton *t = (TButton *) Sender;
        TWinControl *t = (TWinControl *) Sender;
        TWinControl *split = (TWinControl *) Splitter1;
        //split->Left +=10;
        Panel1->Width +=10;

 //SetCursorPos( 20,30);
 ModeEditObject = cDefault;
 switch( Button )
 {
    case mbLeft:
 if( t->Width-7<=X && t->Height-7<=Y )
 {
     Shape1->Left = t->Left-2;
     Shape1->Width=t->Width+4;
     Shape1->Top = t->Top-2;
     Shape1->Height=t->Height+4;
     Shape1->Visible = true;
     ResizeObject = true;
     ModeEditObject = cResize;
     t->Cursor = crCross;
 }
 else
 {
     Shape1->Left = t->Left-2;
     Shape1->Width=t->Width+4;
     Shape1->Top = t->Top-2;
     Shape1->Height=t->Height+4;
     Shape1->Visible = true;
     cX=t->Left-X;
     cY=t->Top-Y;
     ModeEditObject =cMove;
     t->Cursor = crHandPoint;
 }
 break;
        case mbRight:
        break;

 }
 bObject=t;
 //MoveObject = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2ContextPopup(TObject *Sender,
      TPoint &MousePos, bool &Handled)
{
 bObject=(TButton *) Sender;
}
//---------------------------------------------------------------------------



//---------------------------------------------------------------------------

void __fastcall TForm1::Button2MouseUp(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
     Shape1->Visible = false;
     if ( ModeEditObject=cResize )
     {
      bObject->Left = Shape1->Left;
      bObject->Top= Shape1->Top;
     }
     ModeEditObject=cDefault;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2MouseMove(TObject *Sender,
      TShiftState Shift, int X, int Y)
{
 TWinControl *t = (TWinControl *) Sender;
 AnsiString s= Sender->ClassName();

 Label1->Caption = s+" X:"+IntToStr(X)+" Y:"+IntToStr(Y);
 FormInspector->ComboBox1->ItemIndex=2;
 switch( ModeEditObject )
 {
        case cResize:
             t->Width=X;
             t->Height=Y;
             Shape1->Width=t->Width+4;
             Shape1->Height=t->Height+4;
             break;
        case cMove:
        Shape1->Left = cX+X;
        Shape1->Top = cY+Y;
        break;
        default:
         if( t->Width-7<=X && t->Height-7<=Y )
        {
             Shape1->Left = t->Left-2;
             Shape1->Width=t->Width+4;
             Shape1->Top = t->Top-2;
             Shape1->Height=t->Height+4;
             Shape1->Visible = true;
             ResizeObject = true;
             t->Cursor = crCross;
         }
         else
         {
             Shape1->Left = t->Left-2;
             Shape1->Width=t->Width+4;
             Shape1->Top = t->Top-2;
             Shape1->Height=t->Height+4;
             Shape1->Visible = true;
             t->Cursor = crHandPoint;
         }
 }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::PopupMenu1Popup(TObject *Sender)
{
     //
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender)
{
 e1 = new TEdit(Form1);
 e1->Parent = Panel2;
 //b1->OnClick = Button2Click;
 e1->OnMouseMove= Button2MouseMove;
 e1->OnMouseUp = Button2MouseUp;
 e1->OnMouseDown = Button2MouseDown;

 e1->PopupMenu = PopupMenu1;
 e1->PopupMenu->OnPopup = PopupMenu1Popup;

 iObject++;
 e1->Name ="BTest"+IntToStr(iObject);
 e1->Text = "Test"+IntToStr(iObject);

 e1->Visible=true;
 e1->Left =10;
 e1->Top=10;
 e1->Enabled=true;
 listObj.insert(listObj.begin(),e1);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender)
{
 l1 = new TLabel(Form1);
 l1->Parent = Panel2;
 //b1->OnClick = Button2Click;
 l1->OnMouseMove= Button2MouseMove;
 l1->OnMouseUp = Button2MouseUp;
 l1->OnMouseDown = Button2MouseDown;

 l1->PopupMenu = PopupMenu1;
 l1->PopupMenu->OnPopup = PopupMenu1Popup;

 iObject++;
 l1->Name ="BTest"+IntToStr(iObject);
 l1->Caption = "Test"+IntToStr(iObject);

 l1->Visible=true;
 l1->Left =10;
 l1->Top=10;
 l1->Enabled=true;
 listObj.insert(listObj.begin(),l1);
}
//---------------------------------------------------------------------------

